package com.utsirc.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.utsirc.R;
import com.utsirc.UTSirC;
import com.utsirc.irc.IRCService;

public class ConnectActivity extends Activity implements OnClickListener {

	public static ConnectActivity instance;

	private AlertDialog connectDialog;

	@Override
	public void onClick(View v) {

		int viewId = v.getId();

		if (viewId == R.id.connectButton) {

			// hide soft keyboard
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

			// getting required information from user
			EditText nickField = (EditText) findViewById(R.id.nickField);
			EditText serverField = (EditText) findViewById(R.id.serverField);
			String nick = nickField.getText().toString();
			String ip = serverField.getText().toString();

			connectDialog = newConnectingDialog();
			connectDialog.show();

			// login
			UTSirC.instance.login(nick, ip);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		instance = this;
		setTheme(android.R.style.Theme_DeviceDefault_Light_NoActionBar);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.connect);

		Button connectButton = ((Button) findViewById(R.id.connectButton));
		connectButton.setOnClickListener(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onDestroy() {
		Log.d("ConnectActivity", "onDestroy");
		super.onDestroy();
	}

	public void notifyConnected() {

		connectDialog.hide();

		Intent returnIntent = new Intent();
		returnIntent.putExtra("loginResult", 0);
		setResult(RESULT_OK, returnIntent);
		finish();
	}

	public void notifyError(String error) {

		connectDialog.hide();

		if (error.equals("NICKINUSEEXCEPTION")) {
			newErrorDialog("Nick name already in use").show();
		} else if (error.equals("IOEXCEPTION")) {
			newErrorDialog("Cannot connect to the knot network").show();
		} else if (error.equals("IRCEXCEPTION")) {
			newErrorDialog("Cannot connect to the knot network").show();
		}
	}

	public AlertDialog newConnectingDialog() {

		LayoutInflater inflater = LayoutInflater.from(this);
		View connectingDialogView = inflater.inflate(R.layout.connecting, null,
				false);

		AlertDialog dialog = new Builder(this).setView(connectingDialogView)
				.create();
		dialog.setCancelable(false);
		dialog.setCanceledOnTouchOutside(false);
		return dialog;
	}

	public AlertDialog newErrorDialog(String errMsg) {

		return new Builder(this).setTitle("Error").setMessage(errMsg)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
					}
				}).create();
	}
}
